<?php


namespace app\common\service;

use think\Db;

class CompanyService extends BaseService
{
    /**
     * 获取所有的企业列表
     * @return mixed
     * @throws \Exception
     * @author hongwei 2019-06-27
     */
    public function getAllCompany()
    {
        $sql  = 'select * from basic_company where delete_time = 0 and id >=1 and id <=60 order by id asc';
        $data = Db::query($sql);
        if (!$data) {
            exception('获取企业列表失败！');
        }
        return $data;
    }

    /**
     * 判断是否是实时企业
     * @param $companyId
     * @return bool
     * @author hongwei 2019-07-18
     */
    public function isRealtimeCompany($companyId)
    {
        $sql  = 'select * from basic_company where id = ' . $companyId . ' and delete_time = 0 and realtime_company = 1';
        $data = Db::query($sql);
        if (empty($data)) {
            return false;
        }
        return true;
    }

    /**
     * 根据企业ID获取设施信息
     * @param $companyId
     * @param $field
     * @return bool|mixed
     * @throws \Exception
     * @author hongwei 2019-07-18
     */
    public function getFacilityByCompanyId($companyId, $field)
    {
        //step1:判断是否是实时企业
        $result = $this->isRealtimeCompany($companyId);
        if (!$result) {
            exception('该企业不是实时企业，无法获取设施信息！');
        }

        //step2:获取设施信息
        $sql  = 'select ' . $field . ' from basic_facility where company_id =' . $companyId . ' and delete_time = 0';
        $data = Db::query($sql);
        if (!$data) {
            return false;
        }
        return $data;
    }

    /**
     * 根据企业ID 和 类型 获取设施信息
     * @param integer $companyId 企业ID
     * @param string $type 污染物类型,SO2或NO2
     * @param string $field 查询字段
     * @return bool|mixed
     * @throws \Exception
     */
    public function getFacilityByCompanyIdAndType($companyId, $type = 'SO2', $field = '')
    {
        //step1:判断是否是实时企业
        $result = $this->isRealtimeCompany($companyId);
        if (!$result) {
            exception('该企业不是实时企业，无法获取设施信息！');
        }

        //step2:默认是硫
        $typeConvert = 2;

        //step2:获取设施信息
        if (strtoupper($type) == 'SO2') {
            $typeConvert = 2;
        }
        if (strtoupper($type) == 'NO2') {
            $typeConvert = 1;
        }
        $sql  = 'select ' . $field . ' from basic_facility where company_id =' . $companyId . ' and type = ' . $typeConvert . ' and delete_time = 0';
        $data = Db::query($sql);
        if (!$data) {
            return false;
        }
        return $data;
    }

    /**
     * 根据设施ID获取
     * @param $facilityId
     * @return bool|mixed
     * @author hongwei 2019-07-24
     */
    public function getTypeByFacilityId($facilityId)
    {
        $sql  = 'select type from basic_facility where id =' . $facilityId;
        $data = Db::query($sql);
        if (!$data) {
            return false;
        }
        $type = $data[0]['type'];
        return (int)$type;
    }

    /**
     * 根据排口ID获取企业ID
     * @param $drainId
     * @return bool|int
     * @author hongwei 2019-07-25
     */
    public function getCompanyIdByDrainId($drainId)
    {
        $sql  = 'select company_id from basic_drain where id =' . $drainId;
        $data = Db::query($sql);
        if (!$data) {
            return false;
        }
        $result = $data[0]['company_id'];
        return (int)$result;
    }

    /**
     * 根据排口ID获取公司是否是实时企业
     * @param $drainId
     * @return bool|int
     * @author hongwei 2019-07-25
     */
    public function isRealtimeCompanyByDrainId($drainId)
    {
        $companyId = $this->getCompanyIdByDrainId($drainId);
        $sql       = 'select realtime_company from basic_company where id =' . $companyId;
        $data      = Db::query($sql);
        if (!$data) {
            return false;
        }
        $result = $data[0]['realtime_company'];
        return (int)$result;
    }

    /**
     * 根据排口ID获取设施ID数组
     * @param $drainId
     * @param $type
     * @author hongwei 2019-07-26
     */
    public function getFacilityArrayByDrainId($drainId, $type = 'so2')
    {
        //step1:判断是否是实时企业
        $result = $this->isRealtimeCompanyByDrainId($drainId);
        if ($result == 0) {
            exception('该企业不是实时企业，无法获取设施信息！');
        }

        //step2:默认是硫
        $typeConvert = 2;

        //step2:获取设施信息
        if (strtoupper($type) == 'SO2') {
            $typeConvert = 2;
        }
        if (strtoupper($type) == 'NO2') {
            $typeConvert = 1;
        }
        $sql  = 'select * from basic_facility where drain_id =' . $drainId . ' and type = ' . $typeConvert . ' and delete_time = 0';
        $data = Db::query($sql);
        if (!$data) {
            return false;
        }
        $arr = [];
        foreach ($data as $k => $v) {
            array_push($arr, $v['id']);
        }
        return $arr;
    }

    /**
     * 根据排口ID获取设施信息
     * @param $drainId
     * @param string $type
     * @return bool|mixed
     * @throws \Exception
     */
    public function getFacilitiesByDrainId($drainId)
    {
        //step1:判断是否是实时企业
        $result = $this->isRealtimeCompanyByDrainId($drainId);
        if ($result == 0) {
            exception('该企业不是实时企业，无法获取设施信息！');
        }

        $sql  = 'select * from basic_facility where drain_id =' . $drainId;
        $data = Db::query($sql);
        if (!$data) {
            return false;
        }
        return $data;
    }

    /**
     * 根据设施ID获取排口ID
     * @param $facilityId
     * @return bool|int
     * @author hongwei 2019-07-26
     */
    public function getDrainIdByFacilityId($facilityId)
    {
        $sql  = 'select drain_id from basic_facility where id =' . $facilityId . ' group by drain_id';
        $data = Db::query($sql);
        if (!$data) {
            return false;
        }
        $result = $data[0]['drain_id'];
        return (int)$result;
    }

    /**
     * 获取实时排口ID
     * @return bool|string
     * @author hongwei 2019-08-02
     */
    public function getRealtimeDrainIds()
    {
        $sql    = 'select b.id from basic_company as a right join basic_drain as b on a.id = b.company_id where a.realtime_company = 1';
        $result = $this->pdoFetchAll($sql);
        $newArr = [];
        foreach ($result as $k => $v) {
            array_push($newArr, (int)$v['id']);
        }
        if (!$result) {
            return false;
        }
        $drainIds = implode(",", $newArr);
        return $drainIds;
    }
}